<?php

	/**
	 * Workspace@ Engine application comment!
	 * 
	 * LICENSE
	 * This application is licensed under New BSD license.
	 * Please read complete LICENSE.txt inside of root folder for more details.
	 * 
	 * We wish you good building :) 
	 * 
	 * @category   Application
	 * @package    Bootstrap
	 * @author     Nevio Vesic <nevio@workspaceat.com>
	 * @copyright  2008-2009 Workspace@ group in association with Design strategist
	 */

	/**
	 * @see Engine_Application_Configuration_Frontend
	 */
	require_once "Application/Configuration/Frontend.php";

	class Engine_Plugins_Builder extends Engine_Application_Abstract
	{
		
		const JS_EXT  = ".js";
		
		protected static $frontconf = null;
		
		protected static $frontbase = null;
		
		const BUILD_TEMPLATE_DIR    = ".build_templates";
		
		const PAGES_TEMPLATE_DIR    = "pages";
		
		const PAGE_TEMPLATE         = "page_template";
		
		const HIDE_HTML_TEMPLATE    = "index.html";
		
		const PAGE_EXT              = ".phtml";
		
		const PAGE_HTML_EXT         = ".html";
		
		const LANG_FILE_EXT         = ".xml";

		protected static $tmp       = null;
		
		protected $db = null;
		
		function __construct( $appname = "engine" )
		{
			// Lets go allocate base template path!
			if ( self::$frontconf )
			{
				self::$frontconf = null;
			}
			
			if ( self::$tmp )
			{
				self::$tmp = null;
			}
			
			$a = new Engine_Application_Configuration_Backend( );
			self::$frontconf = $a ->getFrontendInstance();
			
			if ( self::$frontbase )
			{
				self::$frontbase = null;
			}
			
			self::$frontbase = self::$frontconf -> html -> basename . DIRECTORY_SEPARATOR .
			                 self::$plugins -> themes -> getWorkingTheme() . DIRECTORY_SEPARATOR;
			
			if (! is_dir (self::$frontbase))
			{
				throw new Engine_Exception ( "Theme working path inside of builder can't be allocated! Path doesn't exists!" );
			}
			
			$this->db = $this->databaseInstance();
			
		}
		
		
		/**
		 * Generate new page file. filename.phtml inside of valid folder
		 *
		 * @param string $pathto
		 * @param string $filename
		 * @return boolean[true|false]
		 */
		public function createPage( $pathto, $filename )
		{
			$filename = $filename . self::PAGE_EXT;
			
			if (! is_dir( $this->resolveCurrentFrontDir($pathto) ) )
			{
				return false;
			}
			
			$realpathto = $this->resolveCurrentFrontDir($pathto);
			
			if(! $a = fopen($realpathto.$filename, "a+", true) )
			{
				return false;	
			}
			else 
				return true;
			
		}
		
		public function createLanguageFile( $pathto, $filename )
		{
			$filename = $filename . self::LANG_FILE_EXT;
			
			# ok lets get language frontend dir.
			$basepath = self::$plugins->language->getFrontendLanguageBasePath();
			
			if (! is_dir ( $basepath . $pathto ) )
			{
				echo "false";
			}
			
			# ok lets build page file
			$handler = fopen ( $basepath . $pathto . $filename, "a+", true );
			
			if ( is_writeable ( $basepath . $pathto . $filename ) )
			{
				$data = '<?xml version="1.0" encoding="UTF-8"?>
<data></data>';
				$a = fwrite($handler, $data );
				
				if ( $a )
				{
					return true;
				}
				else 
					return false;
			}
			
			return false;
		}
		
		/**
		 * Create language dir.
		 *
		 * @param  string $pathto
		 * @return boolean[true|false]
		 */
		public function createLanguageDir($pathto)
		{
			# ok lets get language frontend dir.
			$basepath = self::$plugins->language->getFrontendLanguageBasePath();
			echo $path;
			$path = $basepath . $pathto;
			
			if ( is_dir ( $path ) )
			{
				return true;
			}
			
			if ( mkdir( $path, 0644) )
			{
				return true;
			}
			
			return false;
		}
		
		
		public function deletePage( $path )
		{

			if (! @fopen ( self::$frontbase . $page . self::PAGE_EXT, "r", true ) )
			{
				return array(
					"status" => false,
					"message" => "Page doesn't exists!"
				);
			}
			$file = self::$frontbase . $page . self::PAGE_EXT;
			
			unlink( $file );
			
			return array(
				"status" => true,
				"message" => "Page has been successfully deleted.. Please wait..."
			);
		}
		
		public function createFrontendDir($basename)
		{
			if (! is_dir ( self::$frontbase . $basename ) )
			{
				if ( @mkdir ( self::$frontbase . $basename, 0644 ) )
				{
					return true;
					
				}else{
					
					return false;
				
				}
			}
			
			return true;
		}
		
		/**
		 * Get working frontend dir.
		 *
		 * @param  string $postpath
		 * @return string
		 */
		public function resolveCurrentFrontDir($postpath)
		{
			return self::$frontbase . $postpath;
		}

	}

